package common              //Number and string functions

import (
    "math/rand";"time"
    "strings"
    "strconv"
    "fmt"
)

const (
    KC_RAND_KIND_NUM   = 0  // 纯数字
    KC_RAND_KIND_LOWER = 1  // 小写字母
    KC_RAND_KIND_UPPER = 2  // 大写字母
    KC_RAND_KIND_ALL   = 3  // 数字、大小写字母
)
 
// 随机字符串
func Srand(size int, kind int) string {
    ikind, kinds, result := kind, [][]int{[]int{10, 48}, []int{26, 97}, []int{26, 65}}, make([]byte, size)
    is_all := kind > 2 || kind < 0
    rand.Seed(time.Now().UnixNano())
    for i :=0; i < size; i++ {
        if is_all { // random ikind
            ikind = rand.Intn(3)
        }
        scope, base := kinds[ikind][0], kinds[ikind][1]
        result[i] = uint8(base+rand.Intn(scope))
    }
    S := string(result)
    //fmt.Printf("random string: %s\n", S)
    return S
}


/* 
 [0.0, 1.0]*x+y 之间的随机浮点数
*/
func Frand(start float32, end float32) float32 {
    x := (end-start)/1.0
    y := start
    f :=  (rand.Float32()*x)+y 
    //fmt.Printf("random float: %f\n", f)
    return f  
}

/*
  [0, end]之间的随机整数
 */
func Irand(end int) int {
    return rand.Intn(end)
}

/*
 获取当前时间戳
*/
func CurTime() string {
    ct := time.Now().Format("2006-01-02 15:04:05")
    return ct
}

/* 整数添加到整型数组，数组自动扩展 */
func AppendInt(x []int, y int) []int {
    var z []int
    zlen := len(x) + 1
    if zlen <= cap(x) {
        z = x[:zlen]
    }else {
        zcap := zlen
        if zcap < 2*len(x) {
            zcap = 2*len(x)
        }
        z = make([]int, zlen, zcap)
        copy(z, x)
    }
    z[len(x)] = y
    return z
}
/*
 数字组成的字符串转换为数组
*/
func Str2IntArr(a string) []int {
    sa := strings.Split(a, " ")
    ia := make([]int, len(sa))
    for i := 0; i < len(sa); i++ {
        //fmt.Printf("%s ", sa[i])
        ia[i], _ = strconv.Atoi(sa[i])
        //fmt.Printf("[%d] ", ia[i])
    }
    return ia
}

func PrintIntArr(a []int) string {
    Str := ""
    for i:=0; i < len(a); i++ {
        s := fmt.Sprintf("%d ", a[i])
        //fmt.Printf("%s", s)
        Str += s
    }
    //fmt.Printf("\n")
    return Str
}